<%@ page import="org.jeecgframework.web.util.StaticValue"%>
<%@ page language="java" import="java.util.*" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title>报价单-修改</title>
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<script type="text/javascript" src="plug-in/ckfinder/ckfinder.js"></script>
</head>
<body>
<t:formvalid formid="formobj" dialog="false" usePlugin="password" layout="table" action="cbsQuotationController.do?doUpdate" beforeSubmit="checkTaskHandle" tiptype="3" callback="subCallBack">
<input type="hidden" name="quotationId" value="${quotation.id }">
<input type="hidden" name="taskId" value="${task.id }">
<input type="hidden" name="instId" value="${instId }">
<input type="hidden" name="state" id="state" value="${quotation.state }"/>
	<div class="subtitle">
		<p>基础信息</p>
<!-- 		 &nbsp; &nbsp; &nbsp; <a class="easyui-linkbutton"  style="margin:auto;" >复制报价单</a> -->
<!-- 		 &nbsp; &nbsp; &nbsp;<a class="easyui-linkbutton"  style="margin:auto;" >下载报价单</a> -->
		<table class="nobordertable noborder">
				<tr>
					<td class="al_r" width="20%">
						<label>报价单编号:</label>
					</td>
					<td width="20%">
							${quotation.quotationNum }
					</td>
					<td class="al_r" width="30%">
					</td>
					<td width="30%">
					</td>
				</tr>
				<tr>
					<td class="al_r" width="20%">
						<label>客户类型:</label>
					</td>
					<td width="20%">
							${bus.clientType == 0?'客户':'第三方' }
					</td>
					<td class="al_r" width="30%">
						<c:if test="${bus.clientType != 0 }"><label >采购方:</label></c:if>
					</td>
					<td width="30%">
						<c:if test="${bus.clientType != 0 }">${bus.procurement.departname }</c:if>
					</td>
				</tr>
				<tr>
				
					<td class="al_r">
						<label>影院名称:</label>
					</td>
					<td>
						${bus.cinema.name }
					</td>
					<td class="al_r">
						<label>影院地址:</label>
					</td>
					<td>
						<span id="cinemaAddr">${bus.cinema.address }</span>
					</td>
				</tr>
				<tr>
					<td class="al_r">
						<label>所属机构:</label>
					</td>
					<td>
						<span id="departName">${bus.cinema.belongOrg.departname }</span>
					</td>
					<td class="al_r">
						<label>影院所属省市:</label>
					</td>
					<td>
						<span id="city">
							${bus.cinema.province.territoryName}
							<c:if test="${not empty bus.cinema.city }">,${bus.cinema.city }</c:if>
						</span>
					</td>
				</tr>
				<tr>
					<td class="al_r">
						<label>联系人名称:</label>
					</td>
					<td>
						${bus.contactName }
					</td>
					<td class="al_r">
						<label>联系人电话:</label>
					</td>
					<td>
						<span id="phone">${bus.contactPhone }</span>
					</td>
				</tr>
				<tr>
					<td class="al_r">
						<label>联系人邮箱：</label>
					</td>
					<td>
						<span id="mail">${bus.contactMail }</span>
					</td>
					<td class="al_r">
						<label>传真:</label>
					</td>
					<td>
						<span id="fax">${bus.contactFax }</span>
					</td>
				</tr>
				<tr>
					<td class="al_r">
						<c:if test="${bus.clientType == 0 }"><label>服务地点：</label></c:if>
					</td>
					<td>
						<c:if test="${bus.clientType == 0 }">${bus.servicePlace }</c:if>
					</td>
					<td class="al_r">
						<c:if test="${bus.clientType == 0 }"><label>是否需要上门服务:</label></c:if>
					</td>
					<td>
                        <c:if test="${bus.clientType == 0 }">
                        <input class="customer" type="checkbox" value="1"
                               <c:if test="${bus.isOnsiteServices eq '1' }">checked="checked"</c:if>
                               name="isOnsiteServices"/>
                        </c:if>
						<%--<c:if test="${bus.clientType == 0 }">${bus.isOnsiteServices eq '1'?'否':'是' }</c:if>--%>
					</td>
				</tr>
				<tr>
				  <td class="al_r">
				  		<label>保修期:</label>
				  </td>
				  <td colspan="3">
				  	${bus.warranty }
				  </td>
				</tr>
				<tr>
				  <td class="al_r">
				  		<c:if test="${bus.clientType == 0 }"><label>服务前提:</label></c:if>
				  </td>
				  <td colspan="3">
				  	<c:if test="${bus.clientType == 0 }">${bus.servicesPremise }</c:if>
				  </td>
				</tr>
				<tr>
				  <td class="al_r">
				  	<c:if test="${bus.clientType != 0 }"><label>备注:</label></c:if>
				  </td>
				  <td colspan="3">
				  	<c:if test="${bus.clientType != 0 }">${bus.comment }</c:if>
				  	
				  </td>
				</tr>
			</table>
	</div>
	
	<div class="subtitle" >
			<p>订购明细</p> &nbsp;
			折扣率：
<!-- 				HD生成报价单时不能修改最终价格。如果是第三方订单，折扣率固定为95%且不能修改， -->
				<c:choose>
					<c:when test="${(quotation.type eq '0' || quotation.type eq '1') && bus.clientType eq '1'}">
						<input type="hidden" name="disCount" id="disCount" value="95">95%
					</c:when>
					<c:otherwise>
						<input type="text" name="disCount" id="disCount" dataType="n1-100" style="width: 50px; text-align: right;" onchange="editDisCount()" value="${quotation.disCount }">%
					</c:otherwise>
				</c:choose>
	</div>
	  <table style="margin-top: 20px; line-height: 35px;" cellpadding="0" cellspacing="1" class="formtable" id="partInfoTab">
	  	<tr align="center">
	       <td width="12%" >配件号</td>
	       <td width="16%">中文描述</td>
	       <td width="12%">单位</td>
	       <td width="12%">数量</td>
	       
	       <td width="12%">零售指导价</td>
	       <td width="12%">折扣价</td>
	       <td width="12%">最终价</td>
	       <td width="12%">价格小计CNY（折扣后）</td>
	       
	     </tr>
	     <c:forEach items="${quotation.quotationDetailList }" var="detail" varStatus="a">
	     	<tr align="center" style="background-color: #FFFFFF;">
				<td>${detail.procurementPart.partInfo.partNo }</td>
				<td>${detail.procurementPart.partInfo.chineseDes }</td>
				<td>${detail.procurementPart.partInfo.marketUnit }</td>
				<td>
					<input name="num" type="text" onchange="editNumOrlastPrice(this,1)" value="${detail.num }" name="num">
					<input type="hidden" name="detailId" value="${detail.id }"/>
				</td>
				<td>
					<input type="hidden" class="unitPrice" value="${detail.unitPrice }">
					¥<fmt:formatNumber value="${detail.unitPrice }"  type="number"/>
				</td>
				<td>
					<input type="hidden" name="disCountPrice" value="${detail.disCountPrice }">
					¥<span class="disCountPrice"><fmt:formatNumber value="${detail.disCountPrice}" type="number"/></span>
				</td>
				<td>
<!-- 					HD生成报价单时不能修改最终价格 -->
					<c:if test="${quotation.type eq '0' || quotation.type eq '1'}">
						<input type="hidden" name="lastPrice" value="${detail.lastPrice }">
						¥<span class="lastPrice"><fmt:formatNumber value="${detail.lastPrice}" type="number"/></span>
					</c:if>
					<c:if test="${quotation.type eq '2' }">
						<input type="text" name="lastPrice" onchange="editNumOrlastPrice(this,2)" value="${detail.lastPrice }">
					</c:if>
				</td>
				<td>
					<input type="hidden" name="total" value="${detail.total }">
					¥<span class="countPrice"><fmt:formatNumber value="${detail.total}" type="number"/></span>
				</td>
			 </tr>
	     </c:forEach>
	     <c:if test="${bus.isOnsiteServices eq '1' }">
	     	<tr align="center" style="background-color: #FFFFFF;" class="clf">
	     		<td>上门服务费</td>
				<td>人工费</td>
				<td>EA</td>
				<td><input type="text" value="${quotation.manpowerNumber }" name="manpowerNumber" onchange="loadSerivcePrice(this,1)"></td>
				<td>¥<fmt:formatNumber value="${quotation.manpowerPrice}" type="number"/></td>
				<td>¥<fmt:formatNumber value="${quotation.manpowerPrice}" type="number"/></td>
				<td>¥<fmt:formatNumber value="${quotation.manpowerPrice}" type="number"/></td>
				<td>¥<span id="manpowerPriceCount"><fmt:formatNumber value="${quotation.manpowerNumber * quotation.manpowerPrice}" type="number"/></span></td>
	     	</tr>
	     	<tr align="center" style="background-color: #FFFFFF;" class="clf">
	     		<td></td>
				<td>差旅费</td>
				<td>EA</td>
				<td><input type="text" value="${quotation.travelNumber }" name="travelNumber"  onchange="loadSerivcePrice(this,2)"></td>
				<td>¥<fmt:formatNumber value="${quotation.travelPrice}" type="number"/></td>
				<td>¥<fmt:formatNumber value="${quotation.travelPrice}" type="number"/></td>
				<td>¥<fmt:formatNumber value="${quotation.travelPrice}" type="number"/></td>
				<td>¥<span id=travelPriceCount><fmt:formatNumber value="${quotation.travelNumber * quotation.travelPrice}" type="number"/></span></td>
	     	</tr>
	     </c:if>
	  </table>
       
	<div class="subtitle" >
			<p>相关设备</p> &nbsp;
		</div>
		  <table style="margin-top: 20px; line-height: 35px;" cellpadding="0" cellspacing="1" class="formtable" id="equipmentTab">
		     <tr align="center">
		       <td>设备SN</td>
		       <td>型号</td>
		       <td>机型</td>
		       <td>发货日期</td>
		       <td>安装日期</td>
		       <td>保期截至日期</td>
		     </tr>
		     
		     <c:forEach items="${quotation.note.paertEsList }" var='paertEs'>
				<tr align="center" style="background-color: #FFFFFF;">
					<td>${paertEs.equipmentSpare.sn }</td>
					<td>${paertEs.equipmentSpare.item }</td>
					<td>${paertEs.equipmentSpare.type.typeDesc }</td>
					<td><fmt:formatDate value="${paertEs.equipmentSpare.sendDate }" pattern="yyyy-MM-dd"/></td>
					<td><fmt:formatDate value="${paertEs.equipmentSpare.installDate }" pattern="yyyy-MM-dd"/></td>
					<td><fmt:formatDate value="${paertEs.equipmentSpare.repairEndDate }" pattern="yyyy-MM-dd"/></td>
				</tr>		     
		     </c:forEach>
		  </table>
		<div class="subtitle" >
			<p>审批记录</p> &nbsp;
		</div>
		  <table style="margin-top: 20px; line-height: 35px;" cellpadding="0" cellspacing="1" class="formtable" >
		     <tr align="center">
		       <td>审批人</td>
		       <td>审批时间</td>
		       <td>操作结果</td>
		     </tr>
		     
		     <c:forEach items="${recordList }" var='record'>
				<tr align="center" style="background-color: #FFFFFF;">
					<td>${record.tsUser.realName }</td>
					<td><fmt:formatDate value="${record.createDate }" pattern="yyyy-MM-dd HH:mm"/></td>
					<td>${record.remark }</td>
				</tr>
		     </c:forEach>
		  </table>
</t:formvalid>
	<div style="text-align: center;margin: 50px;">
		<a class="easyui-linkbutton" onclick="$('#state').val(4);$('#formobj').submit();">保存</a>&nbsp;&nbsp;&nbsp;&nbsp;
		<a class="easyui-linkbutton" onclick="$('#state').val(1);$('#formobj').submit();">提交</a>
	</div>
	
</body>

<script type="text/javascript">
$(function(){
    $("input[name='isOnsiteServices']").click(function(){
        if($(this).is(":checked")){
            $(".clf").show()
        }else{
            $(".clf").hide();
        }
    })
	var quotationType = '${quotation.type}';
	var clientType = '${bus.clientType}';
	if((quotationType == 0 || quotationType == 1) && clientType == 1){
		editDisCount();
	}
	
	var msg = '${message}';
	if(msg != ""){
		top.tip(msg);
	}
})

function checkTaskHandle(){
	var taskId = '${task.id}';
	$.ajax({
	    url: "cbsProcurementNoteController.do?checkTaskHandle",    //请求的url地址
	    dataType: "json",   //返回格式为json
	    async: false, //请求是否异步，默认为异步，这也是ajax重要特性
	    data: { "taskId": taskId },    //参数值
	    type: "post",   //请求方式
	    success: function(data) {
	       	if(data.msg == 'n'){
	       		top.tip("操作失败,该任务已被办理");
	       		//刷新列表
	       		var win = frameElement.api.opener;
	       		win.reloadTable();
	       		frameElement.api.close();
	       		return false;
	       	}else{
	       		return true;
	       	}
	    }
	});
}


	function subCallBack(data){
		var type = '${quotation.type}';
		//提示
		top.tip(data.msg);
		if(type == 2 ||$('#state').val() == '4' ){
			//刷新列表
	 		var win = frameElement.api.opener;
	 		win.reloadTable();
	 		frameElement.api.close();
		}else{
			//自动跳转到报价单页面
			var url = "cbsQuotationController.do?goQuotationSend&taskId=${task.id}&instId=${instId}&quotationId=${quotation.id }";
			window.location.href=url;	
		}
	}
	
	function loadSerivcePrice(num,type){
		if(isNaN($(num).val()) || $(num).val() < 1){
			$(num).val(1);
		}
		var price;
		if(type == 1){
			price = '${quotation.manpowerPrice }';
			$('#manpowerPriceCount').text(formatNumber(Math.round($(num).val() * price)));
		}else if(type == 2){
			price = '${quotation.travelPrice }';
			$('#travelPriceCount').text(formatNumber(Math.round($(num).val() * price)));
		}
	}
	
	function editNumOrlastPrice(num,type){
		var number,disCountPrice,lastPrice,countPrice;
		if(type == 1){
			number = $(num).val();
			if(isNaN(number) || number < 1 || number > 10000){
				$(num).val(1);
				number = 1;
			}
			disCountPrice = $(num).parent().next().next().find('input[class="unitPrice"]').val();
			lastPrice = $(num).parent().next().next().next().find('input').val();
			countPrice = number * lastPrice;
			countPrice = Math.round(countPrice);
			$(num).parent().next().next().next().next().find('input[name="total"]').val(countPrice);
			countPrice = formatNumber(countPrice);
			$(num).parent().next().next().next().next().find('span[class="countPrice"]').text(countPrice);
		}else{
			number = $(num).parent().prev().prev().prev().find('input[name="num"]').val();
			disCountPrice = $(num).parent().prev().find('input[name="disCountPrice"]').val();
			lastPrice = $(num).val();
			if(isNaN(lastPrice) || lastPrice < 1 || lastPrice > 1000000000){
				lastPrice = disCountPrice;
				$(num).val(lastPrice);
			}
			countPrice = number * lastPrice;
			countPrice = Math.round(countPrice);
			$(num).parent().next().find('input[name="total"]').val(countPrice);
			countPrice = formatNumber(countPrice);
			$(num).parent().next().find('span[class="countPrice"]').text(countPrice);
		}
		
		
		
	}
	
	function editDisCount(){
		var flag = '${quotation.type}' == 0 || '${quotation.type}' == 1?true:false;
		var disCount = $('#disCount').val();
		if(isNaN(disCount) || disCount > 100 || disCount < 1){
			disCount = 100;
			$('#disCount').val(100)
		}
		disCount = Number(disCount) / 100;
		$('input[name="num"]').each(function(){
			var price = $(this).parent().next().find('input[class="unitPrice"]').val();
			price = Number(price);
			var disCountPrice = price * disCount;
			//四舍五入
			disCountPrice = Math.round(disCountPrice);
			//总计
			var countPrice = disCountPrice * Number($(this).val());
			$(this).parent().next().next().find('input[name="disCountPrice"]').val(disCountPrice);
			$(this).parent().next().next().next().next().find('input[name="total"]').val(countPrice);
			$(this).parent().next().next().next().find('input').val(disCountPrice);
			//千位符号
			disCountPrice = formatNumber(disCountPrice);
			countPrice = formatNumber(countPrice);
			if(flag){
				$(this).parent().next().next().next().find('span[class="lastPrice"]').text(disCountPrice);
			}
			$(this).parent().next().next().find('span[class="disCountPrice"]').text(disCountPrice);
			$(this).parent().next().next().next().next().find('span[class="countPrice"]').text(countPrice);
		})
	}
	
	function formatNumber(num){  
		return (num.toFixed(0) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
	} 
	
</script>
</html>